﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using MarketOnlineDTO;
using MySql.Data.MySqlClient;

namespace MarketOnlineDAO
{
    public class TinhTrangHoaDonDAO : BaseDataProvider
    {
        MySqlCommand _cmd;

        public Boolean ThemTinhTrangHoaDon(TinhTrangHoaDonDTO tthd)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "insert into TinhTrangHoaDon(TenTinhTrangHD) values(@TenTinhTrangHD)";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@TenTinhTrangHD", tthd.TenTinhTrangHD));

            return ExecuteNonQuery(_cmd);
        }

        public Boolean XoaTinhTrangHoaDon(TinhTrangHoaDonDTO tthd)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "delete from TinhTrangHoaDon where MaTinhTrangHD = @MaTinhTrangHD";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaTinhTrangHD", tthd.MaTinhTrangHD));

            return ExecuteNonQuery(_cmd);
        }

        public Boolean CapNhatBangTinhTrangHoaDon(TinhTrangHoaDonDTO tthd)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "update TinhTrangHoaDon set TenTinhTrangHD = @TenTinhTrangHD where MaTinhTrangHD = @MaTinhTrangHD";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@TenTinhTrangHD", tthd.TenTinhTrangHD));
            _cmd.Parameters.Add(new MySqlParameter("@MaTinhTrangHD", tthd.MaTinhTrangHD));

            return ExecuteNonQuery(_cmd);
        }

        public List<TinhTrangHoaDonDTO> LayDanhSachTinhTrangHoaDon()
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from TinhTrangHoaDon";
            _cmd.CommandType = CommandType.Text;

            DataTable dt = new DataTable();

            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }

            List<TinhTrangHoaDonDTO> list = new List<TinhTrangHoaDonDTO>();

            foreach (DataRow row in dt.Rows)
            {
                TinhTrangHoaDonDTO dto = new TinhTrangHoaDonDTO();
                dto.MaTinhTrangHD = int.Parse(row["MaTinhTrangHD"].ToString());
                dto.TenTinhTrangHD = row["TenTinhTrangHD"].ToString();

                list.Add(dto);
            }

            return list;
        }
    }
}
